package cn.edu.xmu.oomall.customer.controller;

import cn.edu.xmu.oomall.core.util.Common;
import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.customer.model.vo.CartsRetVo;
import cn.edu.xmu.oomall.customer.model.vo.CartsVo;
import cn.edu.xmu.oomall.customer.service.ShoppingCartsService;
import cn.edu.xmu.privilegegateway.annotation.aop.Audit;
import cn.edu.xmu.privilegegateway.annotation.aop.LoginName;
import cn.edu.xmu.privilegegateway.annotation.aop.LoginUser;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author Jiawei Zheng
 * @date 2021-12-04
 */

@RestController
@Slf4j
@RefreshScope
@RequestMapping(value = "/", produces = "application/json;charset=UTF-8")
public class ShoppingCartsController {

    @Autowired
    ShoppingCartsService shoppingCartsService;

    /***
     * 买家获得购物车列表
     * @param userId 用户id
     * @param page 页码
     * @param pageSize 每页数目
     * @return Object
     */
    @ApiOperation(value = "买家获得购物车列表")
    @ApiImplicitParams(value={
            @ApiImplicitParam(paramType = "header", dataType = "String", name = "authorization", value = "用户token", required = true),
            @ApiImplicitParam(name = "page",dataType = "Integer",value = "页码",required = false),
            @ApiImplicitParam(name = "pageSize",dataType = "Integer",value = "每页数目",required = false),
    })
    @ApiResponses(value = {
            @ApiResponse(code = 0, message = "成功")
    })
    @Audit
    @GetMapping(value = "/carts")
    public Object getCarts(
            @LoginUser Long userId,
            @RequestParam(name = "page", required = false) Integer page,
            @RequestParam(name = "pageSize",  required = false) Integer pageSize){
        ReturnObject ret=shoppingCartsService.getCarts(userId, page, pageSize);
        ret = Common.getPageRetVo(ret, CartsRetVo.class);
        return Common.decorateReturnObject(ret);
    }

    /***
     * 买家将商品加入购物车
     * @param userId 用户id
     * @param userName 用户名
     * @param cartsVo 购物车视图
     * @return Object
     */
    @ApiOperation(value = "买家将商品加入购物车")
    @ApiImplicitParams(value={
            @ApiImplicitParam(paramType = "header", dataType = "String", name = "authorization", value = "用户token", required = true),
            @ApiImplicitParam(paramType = "body",dataType = "Object",name = "body",value = "可填写的信息",required = true),
    })
    @ApiResponses(value = {
            @ApiResponse(code = 0, message = "成功")
    })
    @Audit
    @PostMapping(value = "/carts")
    public Object addCarts(@LoginUser Long userId, @LoginName String userName,@Valid @RequestBody CartsVo cartsVo,
                           BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return Common.decorateReturnObject(new ReturnObject(ReturnNo.FIELD_NOTVALID));
        }
        ReturnObject ret=shoppingCartsService.addCarts(userId, userName, cartsVo);
        return Common.decorateReturnObject(ret);
    }

    /***
     * 买家清空购物车
     * @param userId 用户id
     * @return Object
     */
    @ApiOperation(value = "买家清空购物车")
    @ApiImplicitParams(value={
            @ApiImplicitParam(paramType = "header", dataType = "String", name = "authorization", value = "用户token", required = true),
    })
    @ApiResponses(value = {
            @ApiResponse(code = 0, message = "成功")
    })
    @Audit
    @DeleteMapping(value = "/carts")
    public Object clearCarts(@LoginUser Long userId){
        ReturnObject ret=shoppingCartsService.clearCarts(userId);
        return Common.decorateReturnObject(ret);
    }

    /***
     * 买家修改购物车单个商品的数量或规格
     * @param userId 用户id
     * @param id 购物车Id
     * @param cartsVo 购物车视图
     * @return Object
     */
    @ApiOperation(value = "买家修改购物车单个商品的数量或规格")
    @ApiImplicitParams(value={
            @ApiImplicitParam(paramType = "header", dataType = "String", name = "authorization", value = "用户token", required = true),
            @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "id", value = "购物车id", required = true),
            @ApiImplicitParam(paramType = "body",dataType = "Object",name = "body",value = "可填写的信息",required = true),

    })
    @ApiResponses(value = {
            @ApiResponse(code = 0, message = "成功")
    })
    @Audit
    @PutMapping(value = "/carts/{id}")
    public Object updateCarts(@LoginUser Long userId,
                              @LoginName String userName,
                              @PathVariable("id") Long id,
                              @Valid @RequestBody CartsVo cartsVo,
                              BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return Common.decorateReturnObject(new ReturnObject(ReturnNo.FIELD_NOTVALID));
        }
        ReturnObject ret=shoppingCartsService.updateCarts(userId, userName, id, cartsVo);
        return Common.decorateReturnObject(ret);
    }

    /***
     * 买家删除购物车中商品
     * @param id 购物车记录id
     * @return Object
     */
    @ApiOperation(value = "买家删除购物车中商品")
    @ApiImplicitParams(value={
            @ApiImplicitParam(paramType = "header", dataType = "String", name = "authorization", value = "用户token", required = true),
            @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "id", value = "购物车id", required = true),
    })
    @ApiResponses(value = {
            @ApiResponse(code = 0, message = "成功")
    })
    @Audit
    @DeleteMapping(value = "/carts/{id}")
    public Object deleteCarts(
            @LoginUser Long userId,
            @PathVariable("id")Long id){
        ReturnObject ret=shoppingCartsService.deleteCarts(userId, id);
        return Common.decorateReturnObject(ret);
    }

}
